package com.project.easy.common.framework.generator.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.project.easy.common.framework.generator.entity.FrameworkGeneratorDataSource;
import com.project.easy.common.framework.generator.entity.dto.TablesResDto;
import com.project.easy.common.framework.generator.mapper.FrameworkGeneratorDataSourceMapper;
import  com.project.easy.common.util.collection.CollectionUtil;
import  com.project.easy.common.util.dto.TwoApiContextParam;
import  com.project.easy.common.util.exception.business.BusinessException;
import  com.project.easy.common.util.string.StringUtil;
import  com.project.easy.common.util.validator.StringValidator;
import com.project.easy.common.framework.generator.service.IFrameworkGeneratorDataSourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author GreatorMan.cool
 * @since 2021-09-28
 */
@Service
public class FrameworkGeneratorDataSourceServiceImpl extends ServiceImpl<FrameworkGeneratorDataSourceMapper, FrameworkGeneratorDataSource> implements IFrameworkGeneratorDataSourceService {


    @Autowired(required = false)
    private DataSourceConfig dataSourceConfig;

    @Autowired
    private FrameworkGeneratorDataSourceMapper frameworkGeneratorDataSourceMapper;
    /**
     * 查询表数据
     *
     * @param twoApiContextParam 查询参数
     * @return 返回数据库中所有表
     */
    @Override
    public List<TablesResDto> queryTable(TwoApiContextParam twoApiContextParam) {
        if (Objects.isNull(dataSourceConfig)) {
            throw  new BusinessException("模板代码生成没有配置完成");
        }
        String tablesSql = dataSourceConfig.getDbQuery().tablesSql();
        if (StringUtil.isNotBlank(twoApiContextParam.getOne())) {
            if (StringValidator.sqlValidate(twoApiContextParam.getOne())) {
                throw  new BusinessException("参数异常!");
            }
            tablesSql = String.format("%s %s", tablesSql, String.format(" %s like %s", dataSourceConfig.getDbQuery().tableName(), "'%" + twoApiContextParam.getOne() + "%'"));
        }
        if (StringUtil.isNotBlank(twoApiContextParam.getTwo())) {
            if (StringValidator.sqlValidate(twoApiContextParam.getTwo())) {
                throw  new BusinessException("参数异常!");
            }
            tablesSql = String.format("%s %s", tablesSql, String.format(" %s like %s", dataSourceConfig.getDbQuery().tableComment(), "'%" + twoApiContextParam.getTwo() + "%'"));
        }

        List<Map<String, Object>> tableMap = frameworkGeneratorDataSourceMapper.queryTable(tablesSql);
        List<TablesResDto> result = new ArrayList<>(CollectionUtil.listSize(tableMap));
        for (Map<String, Object> table : tableMap) {
            TablesResDto tableInfo = new TablesResDto();
            tableInfo.setTableName((String) table.get(dataSourceConfig.getDbQuery().tableName()));
            tableInfo.setTableDesc((String) table.get(dataSourceConfig.getDbQuery().tableComment()));
            if (StringUtil.isBlank(tableInfo.getTableDesc())) {
                tableInfo.setTableDesc(tableInfo.getTableName());
            }
            result.add(tableInfo);
        }

        return result;
    }
}
